package com.bins.cloud.stream.mq;

import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.MessageQueueSelector;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.common.message.MessageConst;
import org.apache.rocketmq.common.message.MessageQueue;
import org.springframework.messaging.MessageHeaders;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@Slf4j
public class OrderlyMessageQueueSelector implements MessageQueueSelector {

    @Override
    public MessageQueue select(List<MessageQueue> mqs, Message msg, Object arg) {
        Integer id = (Integer) ((MessageHeaders) arg).get(MessageConst.PROPERTY_ORIGIN_MESSAGE_ID);
		//	mqs.size()是这个topic总共的messagequeue的个数，我这里是8，
		//	id % OrderlyExample.tags.length 获取1 2 3 4 5
		// 那么 1 6 11 16 21 。。。 应该是在同一个messageQueue上，并且tag都是tagA
        int index = id % StreamOrderlyProducer.tags.length % mqs.size();
        return mqs.get(index);
    }

}
